package com.lsy.weizhi2.activity;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lsy.weizhi2.JsonObject.ShouCangList;
import com.lsy.weizhi2.R;
import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.config.MyHeight;
import com.lsy.weizhi2.fragment.MyPartakeFragment;
import com.lsy.weizhi2.fragment.MySendFragment;
import com.lsy.weizhi2.mybase.MyBaseActivity;
import com.lsy.weizhi2.mybase.MyTitleView;
import com.lsy.weizhi2.utils.MyOkHttpUtils;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


public class WeiZhiManager extends MyBaseActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {
    private Context context;
    private MyTitleView manager_title;
    private TextView manager_send_tv, manager_partake_tv;
    private View manager_send, manager_partake;
    private ViewPager manager_vp;
    private MySendFragment mySendFragment;
    private MyPartakeFragment myPartakeFragment;
    private List<Fragment> list_f;
    private FragmentManager fragmentManager;
    private PageAdapter adapter;
    private int xx, index;
    private Gson gson;
    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wei_zhi_manager);
        context = WeiZhiManager.this;
        Init();
    }

    private void Init() {
        gson = new Gson();
        manager_title = (MyTitleView) findViewById(R.id.manager_title);
        manager_send_tv = (TextView) findViewById(R.id.manager_send_tv);
        manager_partake_tv = (TextView) findViewById(R.id.manager_partake_tv);
        manager_send = findViewById(R.id.manager_send);
        manager_partake = findViewById(R.id.manager_partake);
        manager_vp = (ViewPager) findViewById(R.id.manager_vp);
        mySendFragment = new MySendFragment();
        myPartakeFragment = new MyPartakeFragment();
        list_f = new ArrayList<>();
        list_f.add(mySendFragment);
        list_f.add(myPartakeFragment);
        fragmentManager = getSupportFragmentManager();
        adapter = new PageAdapter(fragmentManager);
        manager_vp.setAdapter(adapter);
        manager_vp.setCurrentItem(index);
        manager_send_tv.setOnClickListener(this);
        manager_partake_tv.setOnClickListener(this);
        manager_vp.addOnPageChangeListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i("WeiZhiManager", "onResume");
        ShowItem();
        xx = 0;
        ViewTreeObserver vto = manager_title.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (xx < 1) {
                    xx++;
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(manager_title.getLayoutParams());
                            params.height = MyHeight.ACTIONBAR + MyHeight.MyTitleHeight;
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                                manager_title.setLayoutParams(params);
                            }
                        }
                    });
                }
            }
        });

    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i("WeiZhiManager", "onPause");
        xx = 0;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.manager_partake_tv:
                index = 1;
                manager_vp.setCurrentItem(index);
                ShowItem();
                break;
            case R.id.manager_send_tv:
                index = 0;
                manager_vp.setCurrentItem(index);
                ShowItem();
                break;
        }
    }

    private void ShowItem() {
        if (index == 0) {
            manager_send_tv.setTextColor(getResources().getColor(R.color.clicked_ok));
            manager_partake_tv.setTextColor(getResources().getColor(R.color.classify_text));
            manager_send.setVisibility(View.VISIBLE);
            manager_partake.setVisibility(View.INVISIBLE);
        } else if (index == 1) {
            manager_partake_tv.setTextColor(getResources().getColor(R.color.clicked_ok));
            manager_send_tv.setTextColor(getResources().getColor(R.color.classify_text));
            manager_send.setVisibility(View.INVISIBLE);
            manager_partake.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        index = position;
        ShowItem();
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    class PageAdapter extends FragmentPagerAdapter {

        public PageAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {

            return list_f.get(position);
        }

        @Override
        public int getCount() {
            return list_f.size();
        }
    }
}
